﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.AdvTree;
using RLMP = RelationsLMP;
using System.Threading;
using System.IO;

namespace AlarmManagePlatform.UC
{
    public partial class HistoryInfo : UserControl
    {
        public HistoryInfo()
        {
            InitializeComponent();

            InitControls();
            btnSearch_Click(null, null);
        }

        private CheckedInstrumentDetail CheckedInstrumentDetail;
        private ComboBox.User _CmbUser;
        private ComboBox.SubmitUnit _CmbSubmitUnit;
        private ComboBox.Manufacturer _CmbManufacturer;

        private void InitControls()
        {
            dtBegin.Value = DateTime.Now.AddDays(-1);
            dtFinish.Value = DateTime.Now;

            InitContextMenStrip();
            InitComBoxUserInfo();

            if (CheckedInstrumentDetail == null)
            {
                CheckedInstrumentDetail = new CheckedInstrumentDetail();
                //CheckedInstrumentDetail.Event_UpdateImageResult += CheckedInstrumentDetail_Event_UpdateImageResult;
                CheckedInstrumentDetail.Dock = DockStyle.Fill;
                gpCheckedInstrumentDetail.Controls.Add(CheckedInstrumentDetail);
            }

            dgvAlarmInstrumentInfo.CellClick += DgvAlarmInstrumentInfo_CellClick;
            Static.Methods.CreateDGVColumns(ref dgvAlarmInstrumentInfo, Model.SystemEnum.DGVType.AlarmDetailType);
            dgvAlarmInstrumentInfo.Columns["Longitude"].Visible = false;
            dgvAlarmInstrumentInfo.Columns["Latitude"].Visible = false;
            dgvAlarmInstrumentInfo.Columns["CreateDate"].Visible = false;

            treeCheckData.Dock = DockStyle.Fill;
            treeCheckData.Nodes.Clear();
            treeCheckData.DragDropEnabled = false;
            treeCheckData.NodeClick += TreeCheckData_NodeClick;

            pagTree.PageSize = 4;
            pagTree.Event_SearchDataInfo += PagTree_Event_SearchDataInfo;
            pagTree.Event_UpdatePagingControl += PagTree_Event_UpdatePagingControl;

            pagInstrument.PageSize = 20;
            pagInstrument.Event_SearchDataInfo += PagInstrument_Event_SearchDataInfo;
            pagInstrument.Event_UpdatePagingControl += btnSearch_Click;

            _CmbUser = new ComboBox.User();
            _CmbUser.Dock = DockStyle.Fill;
            pUser.Controls.Add(_CmbUser);

            _CmbSubmitUnit = new ComboBox.SubmitUnit();
            _CmbSubmitUnit.Dock = DockStyle.Fill;
            pSubmitUnit.Controls.Add(_CmbSubmitUnit);

            _CmbManufacturer = new ComboBox.Manufacturer();
            _CmbManufacturer.Dock = DockStyle.Fill;
            pManufacturer.Controls.Add(_CmbManufacturer);
        }

        private void InitComBoxUserInfo()
        {
            try
            {
                Static.Methods.InitComBox(ref cmbUser, Model.SystemEnum.ComBoxType.UserInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "初始化ComBoxUserInfo异常，错误信息：" + ex.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void TreeCheckData_NodeClick(object sender, TreeNodeMouseEventArgs e)
        {
            if (e.Node == null || e.Node.Level != 1) return;
            string _InstrumentId; int _HIS_IndexId;
            Model.HisCreateTimeInfo _HisCreateTimeInfo;
            Model.CheckDataInfo _CheckDataInfo;

            _InstrumentId = dgvAlarmInstrumentInfo.SelectedRows[0].Cells["InstrumentId"].Value.ToString();
            _HisCreateTimeInfo = (Model.HisCreateTimeInfo)e.Node.Parent.Tag;
            _CheckDataInfo = (Model.CheckDataInfo)e.Node.Tag;

            if (_CheckDataInfo != null)
                _HIS_IndexId = _CheckDataInfo.IndexId;
            else
                _HIS_IndexId = 0;
            if (CheckedInstrumentDetail != null)
                CheckedInstrumentDetail.ShowCheckedInstrumentDetail(_InstrumentId, _HIS_IndexId, _HisCreateTimeInfo.CreateTime, _CheckDataInfo.TestValue);
        }

        private void DgvAlarmInstrumentInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            string _InstrumentId = dgvAlarmInstrumentInfo.SelectedRows[0].Cells["InstrumentId"].Value.ToString();

            int _CreateTimeCount = -1;

            Dictionary<string, string> _LogisticList = GetLogisticList();

            _CreateTimeCount = new DataAccess.tb_AlarminstrumentHisData().GetInstrumentHisDataListCount(_InstrumentId, _LogisticList);
            pagTree.IniEnabled(_CreateTimeCount);

            if (sender != null && e.RowIndex >= 0)
            {
                if (dgvAlarmInstrumentInfo.Rows[e.RowIndex].Cells["Selected"].Value != null && (bool)dgvAlarmInstrumentInfo.Rows[e.RowIndex].Cells["Selected"].Value)
                    dgvAlarmInstrumentInfo.Rows[e.RowIndex].Cells["Selected"].Value = false;
                else
                    dgvAlarmInstrumentInfo.Rows[e.RowIndex].Cells["Selected"].Value = true;
            }

            if (CheckedInstrumentDetail != null)
                CheckedInstrumentDetail.ClealAll();
        }

        /// <summary>
        /// 菜单
        /// </summary>
        private ContextMenuStrip contextMenuStrip;

        private void InitContextMenStrip()
        {
            if (contextMenuStrip == null)
                contextMenuStrip = new ContextMenuStrip();
            try
            {
                contextMenuStrip.Items.Add("删除", null, new EventHandler((c, e1) =>
                {
                    //删除功能
                    //if (Common.StaticInfo.OperatorInfo.OperatorType != RelationsLMPModel.SystemEnum.OperatorType.Administrator)
                    //{
                    //    MessageBox.Show(this, "当前操作员无使用此功能的权限！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //    return;
                    //}
                    if (MessageBox.Show("是否确认删除？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                    {
                        Model.HisCreateTimeInfo _HisCreateTimeInfo;
                        string _InsturmentId = dgvAlarmInstrumentInfo.SelectedRows[0].Cells["InstrumentId"].Value.ToString();
                        _HisCreateTimeInfo = treeCheckData.SelectedNode.Tag as Model.HisCreateTimeInfo;
                        //tb_AlarminstrumentHisDeailData
                        new DataAccess.tb_AlarminstrumentHisDeailData().DeleteAlarminstrumentHisDeail(_InsturmentId, _HisCreateTimeInfo.CreateTime);
                        //tb_AlarminstrumentHisData
                        new DataAccess.tb_AlarminstrumentHisData().DeleteAlarminstrumentHis(_InsturmentId, _HisCreateTimeInfo.CreateTime);

                        DgvAlarmInstrumentInfo_CellClick(null, new DataGridViewCellEventArgs(0, dgvAlarmInstrumentInfo.SelectedRows[0].Index));
                    }
                }));
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除检测数据失败，错误信息：" + ex.Message);
            }
        }

        private void PagTree_Event_UpdatePagingControl(object arg1, EventArgs arg2)
        {
            DgvAlarmInstrumentInfo_CellClick(null, new DataGridViewCellEventArgs(0, dgvAlarmInstrumentInfo.SelectedRows[0].Index));
        }

        private void PagTree_Event_SearchDataInfo(int pageIndex, int pageSize)
        {
            DevComponents.AdvTree.Node _NodeInfo, _NodeInfo1;
            DevComponents.AdvTree.Cell _CellInfo;
            DevComponents.AdvTree.ColumnHeader _ColumnsHeader;

            int _StartIndex = (pageIndex * pageSize) + 1;
            int _SearchCount = pageSize;
            try
            {

                if (dgvAlarmInstrumentInfo.Rows.Count <= 0)
                {
                    return;
                }

                string _InstrumentId = dgvAlarmInstrumentInfo.SelectedRows[0].Cells["InstrumentId"].Value.ToString();

                Dictionary<string, string> _LogisticList = GetLogisticList();

                List<Model.HisCreateTimeInfo> _CreateTimeList;

                _CreateTimeList = new DataAccess.tb_AlarminstrumentHisData().GetInstrumentHisDataList(_InstrumentId, _StartIndex, _SearchCount, _LogisticList);

                List<Model.CheckDataInfo> _CheckDataInfoList;
                List<RelationsLMP.Model.InstrumentParaInfo> _InstrumentParaInfoList = null;

                _InstrumentParaInfoList = new List<RelationsLMP.Model.InstrumentParaInfo>()
            {
                new RelationsLMP.Model.InstrumentParaInfo()
                {
                    ParaId = 33,
                    Code = "TestItems",
                    Description = "检测项目"
                },
                new RelationsLMP.Model.InstrumentParaInfo()
                {
                    ParaId = 38,
                    Code = "Result",
                    Description = "结果"
                },
                new RelationsLMP.Model.InstrumentParaInfo()
                {
                    ParaId = 39,
                    Code = "Average",
                    Description = "平均值"
                }
            };

                treeCheckData.Nodes.Clear();

                for (int i = 0; i < _CreateTimeList.Count; i++)
                {
                    _NodeInfo = new DevComponents.AdvTree.Node();
                    _NodeInfo.Tag = _CreateTimeList[i];
                    _NodeInfo.Text = _CreateTimeList[i].ParaInfo == null ? _CreateTimeList[i].CreateTime : _CreateTimeList[i].CreateTime + " / " + _CreateTimeList[i].ParaInfo.Desc;

                    foreach (RelationsLMP.Model.InstrumentParaInfo _TempInfo in _InstrumentParaInfoList)
                    {
                        _ColumnsHeader = new DevComponents.AdvTree.ColumnHeader();
                        _ColumnsHeader.Name = _TempInfo.Code;
                        _ColumnsHeader.Text = _TempInfo.Description;

                        if (_TempInfo.ParaId == 33)
                        {
                            _ColumnsHeader.Width.Absolute = 120;
                        }
                        else
                        {
                            _ColumnsHeader.Width.Absolute = 60;
                        }
                        _NodeInfo.NodesColumns.Add(_ColumnsHeader);
                    }

                    _CheckDataInfoList = new DataAccess.tb_AlarminstrumentHisData().GetCheckDataInfo(_InstrumentId, _CreateTimeList[i].CreateTime, _InstrumentParaInfoList);

                    for (int j = 0; j < _CheckDataInfoList.Count; j = j + _InstrumentParaInfoList.Count)
                    {
                        _NodeInfo1 = new DevComponents.AdvTree.Node();
                        _NodeInfo1.Name = _InstrumentParaInfoList[0].Code;

                        for (int m = 1; m < _InstrumentParaInfoList.Count; m++)
                        {
                            _CellInfo = new DevComponents.AdvTree.Cell();
                            _CellInfo.Name = _InstrumentParaInfoList[m].Code;
                            _NodeInfo1.Cells.Add(_CellInfo);
                        }
                        _NodeInfo1.Text = _CheckDataInfoList[j].ParaValue;
                        _NodeInfo1.Tag = _CheckDataInfoList[j];

                        for (int m = 1; m < _InstrumentParaInfoList.Count; m = m + _InstrumentParaInfoList.Count)
                        {
                            for (int n = m; n < _InstrumentParaInfoList.Count; n++)
                            {
                                if (j + n < _CheckDataInfoList.Count)
                                    _NodeInfo1.Cells[_InstrumentParaInfoList[n].Code].Text = _CheckDataInfoList[j + n].ParaValue;
                                else
                                    _NodeInfo1.Cells[_InstrumentParaInfoList[n].Code].Text = string.Empty;
                            }
                        }
                        _NodeInfo.Nodes.Add(_NodeInfo1);
                    }
                    _NodeInfo.ContextMenu = contextMenuStrip;
                    treeCheckData.Nodes.Add(_NodeInfo);
                }

                if (treeCheckData.Nodes.Count > 0)
                {
                    treeCheckData.SelectedNode = treeCheckData.Nodes[0];
                    treeCheckData.SelectedNode.Expand();
                }

                if (CheckedInstrumentDetail != null)
                    CheckedInstrumentDetail.ClealAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "显示检测数据失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void PagInstrument_Event_SearchDataInfo(int pageIndex, int pageSize)
        {
            int _StartIndex = (pageIndex * pageSize) + 1;
            int _SearchCount = pageSize;

            Dictionary<string, string> _LogisticList = GetLogisticList();

            List<Model.DB.tb_AlarminstrumentInfo> _ListInstrument = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentList_History(_StartIndex, _SearchCount, _LogisticList);

            dgvAlarmInstrumentInfo.DataSource = _ListInstrument;

            if (dgvAlarmInstrumentInfo.SelectedRows.Count <= 0 && dgvAlarmInstrumentInfo.Rows.Count > 0)
            {
                dgvAlarmInstrumentInfo.Rows[0].Selected = true;
            }

            if (dgvAlarmInstrumentInfo.SelectedRows.Count > 0)
                DgvAlarmInstrumentInfo_CellClick(null, new DataGridViewCellEventArgs(0, dgvAlarmInstrumentInfo.SelectedRows[0].Index));
            else
            {
                treeCheckData.Nodes.Clear();
                pagTree.IniEnabled(0);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                int _DataCount;
                Dictionary<string, string> _LogisticList;

                try
                {
                     _LogisticList = GetLogisticList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "获取查询条件失败，提示信息：" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    _CmbSubmitUnit.Focus();
                    return;
                }

                _DataCount = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentCount(_LogisticList);
                pagInstrument.IniEnabled(_DataCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "查询失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> _IndexList = new List<string>();
            bool _Select;
            for (int i = 0; i < dgvAlarmInstrumentInfo.Rows.Count; i++)
            {
                if (dgvAlarmInstrumentInfo.Rows[i].Cells["Selected"].Value == null) continue;
                _Select = (bool)dgvAlarmInstrumentInfo.Rows[i].Cells["Selected"].Value;
                if (_Select)
                    _IndexList.Add(dgvAlarmInstrumentInfo.Rows[i].Cells["InstrumentId"].Value.ToString());
            }

            if (_IndexList.Count <= 0)
            {
                MessageBox.Show(this, "请选择报警器！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("是否确认删除？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                try
                {
                    //由简单到深删除
                    //tb_AlarminstrumentHisDeailData
                    new DataAccess.tb_AlarminstrumentHisDeailData().DeleteAlarminstrumentHisDeail(_IndexList);
                    //tb_AlarminstrumentHisData
                    new DataAccess.tb_AlarminstrumentHisData().DeleteAlarminstrumentHis(_IndexList);
                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "删除检测数据失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            List<string> _IndexList = new List<string>();
            bool _Select;
            for (int i = 0; i < dgvAlarmInstrumentInfo.Rows.Count; i++)
            {
                if (dgvAlarmInstrumentInfo.Rows[i].Cells["Selected"].Value == null) continue;
                _Select = (bool)dgvAlarmInstrumentInfo.Rows[i].Cells["Selected"].Value;
                if (_Select)
                    _IndexList.Add(dgvAlarmInstrumentInfo.Rows[i].Cells["InstrumentId"].Value.ToString());
            }

            if (_IndexList.Count <= 0)
            {
                MessageBox.Show(this, "请选择报警器！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            RelationsLMP.Control.Form.WaitForm.ShowSplashScreen();
            RelationsLMP.Control.Form.WaitForm.ShowRuningMessage("正在导出，请稍等......");

            Thread _ExportExcel = new Thread(new ParameterizedThreadStart(Thread_ExportExcel));
            _ExportExcel.IsBackground = true;
            _ExportExcel.Start(_IndexList);

        }

        private void Thread_ExportExcel(object para)
        {
            List<string> _IndexID = para as List<string>;

            try
            {
                for (int i = 0; i < _IndexID.Count; i++)
                {
                    CreateExcelInfo(_IndexID[i]);
                }

                if (MessageBox.Show("导出完成，是否打开目录？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    System.Diagnostics.Process.Start(Static.ParaInfo.ReportHomePath);
            }
            catch (Exception ex)
            {
                //MessageBox.Show( "检定信息导出异常，请稍后重试！" + ex.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (RelationsLMP.Control.Form.WaitForm.Instance != null)
                {
                    RelationsLMP.Control.Form.WaitForm.Instance.BeginInvoke(new MethodInvoker(RelationsLMP.Control.Form.WaitForm.Instance.Dispose));
                    RelationsLMP.Control.Form.WaitForm.Instance = null;
                }
            }
        }

        private void CreateExcelInfo(string instrumentId)
        {
            string _InstrumentId = instrumentId;

            //仪表信息

            Model.DB.tb_AlarminstrumentInfo AlarminstrumentInfo;
            AlarminstrumentInfo = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentForInstrumentIdSingle(_InstrumentId);

            System.Data.DataSet ds = new System.Data.DataSet();
            System.Data.DataTable dt;

            #region 创建仪器信息表

            dt = new DataTable();
            dt.Columns.Add("仪器名称", typeof(string));
            //dt.Columns.Add("仪器类型", typeof(string));
            dt.Columns.Add("出厂编号", typeof(string));
            dt.Columns.Add("型号", typeof(string));
            //dt.Columns.Add("量程", typeof(string));
            dt.Columns.Add("合同编号", typeof(string));
            dt.Columns.Add("送检单位", typeof(string));
            dt.Columns.Add("制造商", typeof(string));
            dt.Columns.Add("创建日期", typeof(string));

            DataRow drInfo = dt.NewRow();
            drInfo[0] = AlarminstrumentInfo.InstrumentName;
            //drInfo[1] = AlarminstrumentInfo.InstrumentType;
            drInfo[1] = AlarminstrumentInfo.FactoryNumber;
            drInfo[1] = AlarminstrumentInfo.Model;
            //drInfo[4] = RelationsLMP.Common.Methods.RangeToString(AlarminstrumentInfo.Range);
            drInfo[3] = AlarminstrumentInfo.ContractNo;
            drInfo[4] = AlarminstrumentInfo.SubmitUnit;
            drInfo[5] = AlarminstrumentInfo.Manufacturer;
            drInfo[6] = AlarminstrumentInfo.CreateDate;

            dt.Rows.Add(drInfo);
            ds.Tables.Add(dt);

            #endregion 创建仪器信息表


            Dictionary<string, string> _LogisticList = GetLogisticList();
            //监测时间
            List<Model.HisCreateTimeInfo> _CreateTimeList;

            _CreateTimeList = new DataAccess.tb_AlarminstrumentHisData().GetInstrumentHisDataList(_InstrumentId, 1, 9999, _LogisticList);

            //RelationsLMPModel.HisCreateTimeInfo[] _CreateTimeList = new DataAccess.M_H_CheckedInstrumentHis().GetCheckedInstrumentHisCreateTime(_Id);


            //检测数据
            List<Model.CheckDataInfo> _CheckDataInfoList;

            //改造数据表
            for (int i = 0; i < _CreateTimeList.Count; i++)
            {
                dt = new DataTable();
                foreach (RelationsLMP.Model.InstrumentParaInfo _TempInfo in Static.ParaInfo.InstrumentParaInfoList)
                {
                    dt.Columns.Add(_TempInfo.Description, typeof(string));
                }

                _CheckDataInfoList = new DataAccess.tb_AlarminstrumentHisData().GetCheckDataInfo(_InstrumentId, _CreateTimeList[i].CreateTime, Static.ParaInfo.InstrumentParaInfoList);

                for (int j = 0; j < _CheckDataInfoList.Count; j = j + Static.ParaInfo.InstrumentParaInfoList.Count)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = _CheckDataInfoList[j].ParaValue;
                    for (int m = 1; m < Static.ParaInfo.InstrumentParaInfoList.Count; m = m + Static.ParaInfo.InstrumentParaInfoList.Count)
                    {
                        for (int n = m; n < Static.ParaInfo.InstrumentParaInfoList.Count; n++)
                        {
                            if (j + n < _CheckDataInfoList.Count)
                                dr[n] = _CheckDataInfoList[j + n].ParaValue;
                            else
                                dr[n] = string.Empty;
                        }
                    }
                    dt.Rows.Add(dr);
                }
                ds.Tables.Add(dt);
            }

            CreateExcel(AlarminstrumentInfo, _CreateTimeList, ds);
        }

        private void CreateExcel(Model.DB.tb_AlarminstrumentInfo _M_D_CheckedInstrument, List<Model.HisCreateTimeInfo> _CreateTimeList, DataSet ds)
        {
            string _ReportTemplate = Static.ParaInfo.ReportTemplateHomePath + "\\ExportDataTemplate.xlsx";
            string _TargetFile = Static.ParaInfo.ReportHomePath + _M_D_CheckedInstrument.InstrumentName + "_" + _M_D_CheckedInstrument.FactoryNumber + "_" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
            try
            {
                try
                {
                    if (File.Exists(_TargetFile))
                        File.Delete(_TargetFile);
                    File.Copy(_ReportTemplate, _TargetFile);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                List<string> _SheetName = new List<string>();
                _SheetName.Add("仪器信息");

                foreach (Model.HisCreateTimeInfo Name in _CreateTimeList)
                {
                    _SheetName.Add(Convert.ToDateTime(Name.CreateTime).ToString("yyyyMMdd HHmmss"));
                }

                RelationsLMP.Common.Methods.ExportExcelTemplate(_TargetFile, ds, _SheetName);
            }
            catch (Exception ex)
            {
                throw new Exception("创建导出文件失败，错误信息：" + ex.Message);
            }
        }

        private Dictionary<string, string> GetLogisticList()
        {
            Dictionary<string, string> _Result = new Dictionary<string, string>();

            //若送检单位信息不存在，抛出异常
            //_Result.Add("SubmitUnit", checkSubmitUnit.Checked ? cmbSubmitUnit.Text.ToString() : string.Empty);
            _Result.Add("SubmitUnit", checkSubmitUnit.Checked ? _CmbSubmitUnit.Text.ToString() : string.Empty);

            //_Result.Add("Manufacturer", checkManufacturer.Checked ? txtManufacturer.Text.ToString() : string.Empty);
            _Result.Add("Manufacturer", checkManufacturer.Checked ? _CmbManufacturer.Text.ToString() : string.Empty);

            _Result.Add("BeginTime", checkDateTime.Checked ? dtBegin.Value.ToString(Static.ParaInfo.DateFormatShort + " 00:00:00") : string.Empty);
            _Result.Add("FinishTime", checkDateTime.Checked ? dtFinish.Value.ToString(Static.ParaInfo.DateFormatShort + " 23:59:59") : string.Empty);

            _Result.Add("FactoryNumber", checkFactoryNumber.Checked ? txtFactoryNumber.Text.ToString() : string.Empty);

            List<string> _GasType = new List<string>();

            if (checkO2.Checked)
                _GasType.Add("41");
            if (checkH2S.Checked)
                _GasType.Add("47");
            if (checkCO.Checked)
                _GasType.Add("49");
            if (checkCH4.Checked)
                _GasType.Add("50");
            if (_GasType.Count > 0)
                _Result.Add("GasTypeList", string.Join("%", _GasType.ToArray()));
            else
                _Result.Add("GasTypeList", string.Empty);

            if (checkSampingMode.Checked)
            {
                RelationsLMP.Model.SystemEnum.AlertorSampingMode _AlertorSampingMode = (RelationsLMP.Model.SystemEnum.AlertorSampingMode)cmbSampingMode.SelectedValue;
                _Result.Add("SampingMode", ((int)_AlertorSampingMode).ToString());
            }
            else
                _Result.Add("SampingMode", string.Empty);

            _Result.Add("UserId", checkUser.Checked ? cmbUser.SelectedValue.ToString() : string.Empty);

            return _Result;
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            if (dgvAlarmInstrumentInfo.SelectedRows.Count <= 0)
            {
                MessageBox.Show(this, "请选择报警器信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (treeCheckData.SelectedNode == null)
            {
                MessageBox.Show(this, "请选择需要生成报告的检定数据！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            bool _IsUpload = false;
            Thread _CreateReportTH = new Thread(new ParameterizedThreadStart(Thread_CreateReport));
            _CreateReportTH.IsBackground = true;
            _CreateReportTH.Start(_IsUpload);
        }


        private void Thread_CreateReport(object para)
        {
            try
            {
                Model.DB.tb_AlarminstrumentInfo _AlarminstrumentInfo;
                Model.HisCreateTimeInfo _HisCreateTimeInfo;
                string _InstrumentId;
                bool _IsUpload = (bool)para;

                RelationsLMP.Control.Form.WaitForm.ShowSplashScreen();
                RelationsLMP.Control.Form.WaitForm.ShowRuningMessage("正在生成报告，请稍等......");

                _InstrumentId = dgvAlarmInstrumentInfo.SelectedRows[0].Cells["InstrumentId"].Value.ToString();

                if (treeCheckData.SelectedNode.Level == 0)
                    _HisCreateTimeInfo = treeCheckData.SelectedNode.Tag as Model.HisCreateTimeInfo;
                else
                    _HisCreateTimeInfo = treeCheckData.SelectedNode.Parent.Tag as Model.HisCreateTimeInfo;

                if (_HisCreateTimeInfo != null)
                {
                    _AlarminstrumentInfo = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentForInstrumentIdSingle(_InstrumentId);

                    Static.Methods.CreateReport(_AlarminstrumentInfo, _HisCreateTimeInfo);

                    if (MessageBox.Show("检定报告生成完成，是否打开检定报告保存目录？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                        System.Diagnostics.Process.Start(Static.ParaInfo.ReportHomePath);
                }
                else
                {
                    if (this.InvokeRequired)
                    {
                        this.Invoke(new Action(() =>
                        {
                            MessageBox.Show(this, "选择导出时间有误，请重新选择！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }));
                    }
                    else
                    {
                        MessageBox.Show(this, "选择导出时间有误，请重新选择！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (RelationsLMP.Control.Form.WaitForm.Instance != null)
                {
                    RelationsLMP.Control.Form.WaitForm.Instance.BeginInvoke(new MethodInvoker(RelationsLMP.Control.Form.WaitForm.Instance.Dispose));
                    RelationsLMP.Control.Form.WaitForm.Instance = null;
                }
            }
        }
    }
}
